<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1000" destroy-on-close @closed="$emit('closed')" :align-center="true" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="110px" label-position="right">
			<div class="sketch_content">
				<el-row :gutter="1" >
					<el-col :span="8">
						<el-form-item label="客户全称" prop="apply_name">
							<el-input v-model="form.apply_name" placeholder="请输入客户全称" clearable autocomplete="off"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item label="客户简称" prop="referred_as">
							<el-input v-model="form.referred_as" placeholder="请输入客户简称" clearable autocomplete="off"></el-input>
						</el-form-item>
					</el-col>
                    <el-col :span="8">
						<el-form-item label="税号" prop="tax_identification_no">
							<el-input v-model="form.tax_identification_no" placeholder="请输入税号" clearable autocomplete="off"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="1" >
					<el-col :span="8">
						<el-form-item label="联系人" prop="linkman">
							<el-input v-model="form.linkman" placeholder="请输入联系人" clearable></el-input>
						</el-form-item>
					</el-col>
                    <el-col :span="8">
						<el-form-item label="联系方式" prop="mobile">
							<el-input v-model="form.mobile" placeholder="请输入联系方式" clearable></el-input>
						</el-form-item>
					</el-col>
                    <el-col :span="8">
						<el-form-item label="联系地址" prop="address">
							<el-input v-model="form.address" placeholder="请输入联系地址" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="1" >
					<el-col :span="8">
						<el-form-item label="客户交期时长" prop="custom_delivery_time">
                            <el-input-number v-model="form.custom_delivery_time" :min="0" />
						</el-form-item>
					</el-col>
                    <el-col :span="8">
						<el-form-item label="生产交期时长" prop="produce_delivery_time">
                            <el-input-number v-model="form.produce_delivery_time" :min="0" />
						</el-form-item>
					</el-col>
                    <el-col :span="8">
						<el-form-item label="出货允差" prop="sell_tolerance">
                            <el-input-number v-model="form.sell_tolerance" :min="0" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="1" >
                    <el-col :span="8">
						<el-form-item label="结算方式" prop="settlement_way">
                            <el-select v-model="form.settlement_way"  filterable style="width: 100%" placeholder="请选择结算方式">
								<el-option v-for="item in Settlement" :key="item.id" :label="item.title" :value="item.id"/>
							</el-select>
						</el-form-item>
					</el-col>
                    <el-col :span="8">
						<el-form-item label="母单发货" prop="is_master_order_send">
                            <el-select v-model="form.is_master_order_send"  filterable style="width: 100%" placeholder="请选择是否母单发货">
								<el-option v-for="item in muDan" :key="item.id" :label="item.name" :value="item.id"/>
							</el-select>
						</el-form-item>
					</el-col>
                    <el-col :span="8">
						<el-form-item label="客户等级" prop="grade">
                            <el-input v-model="grade" placeholder="请输入联系方式" readonly></el-input>
						</el-form-item>
					</el-col>
				</el-row>
                <el-row :gutter="1" >
                    <el-col :span="8">
						<el-form-item label="品牌" prop="brand_str">
                            <el-select v-model="form.brand_str" multiple placeholder="请选择品牌" style="width: 240px">
                                <el-option v-for="item in cates" :key="item.id" :label="item.brand_name" :value="item.id" />
                            </el-select>
						</el-form-item>
					</el-col>
                    <el-col :span="8">
						<el-form-item label="简介" prop="desc">
                            <el-input v-model="form.desc" clearable placeholder="请输入简介"></el-input>
                        </el-form-item>
					</el-col>
                    <el-col :span="8">
                        <el-form-item label="是否禁用" prop="system_disable">
                            <el-switch v-model="form.system_disable" :active-value="1" :inactive-value="0"></el-switch>
                        </el-form-item>
					</el-col>
				</el-row>
                <el-row :gutter="1">
                    <el-col :span="8">
                        <el-form-item label="是否打印公司名" prop="has_company">
                            <el-switch v-model="form.has_company" :active-value="1" :inactive-value="0"></el-switch>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="是否打印客户名" prop="has_custom">
                            <el-switch v-model="form.has_custom" :active-value="1" :inactive-value="0"></el-switch>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="是否打印成分" prop="has_component">
                            <el-switch v-model="form.has_component" :active-value="1" :inactive-value="0"></el-switch>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="是否打印订单号" prop="has_orderno">
                            <el-switch v-model="form.has_orderno" :active-value="1" :inactive-value="0"></el-switch>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="是否打印SKC" prop="has_skc">
                            <el-switch v-model="form.has_skc" :active-value="1" :inactive-value="0"></el-switch>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="1">
                    <el-form-item label="出货要求" prop="sell_ask">
                        <sc-editor :disabled="mode=='show'" v-model="form.sell_ask" placeholder="请输入" :height="350"></sc-editor>
                    </el-form-item>
                </el-row>
			</div>
		</el-form>
		<template #footer>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
			<el-button @click="visible=false" >取 消</el-button>
		</template>
	</el-dialog>
</template>

<script>
import { defineAsyncComponent } from 'vue';
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
	export default {
		components: {
			scEditor
		},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '添加客户',
					edit: '编辑客户',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
                grade:"潜力",
				//表单数据
				form: {
					custom_id:"",
					apply_name: "",
					referred_as:"",
					mobile: "",
					linkman: "",
					tax_identification_no: "",
					settlement_way:"",
					system_disable:0,
					address:'',
					desc:'',
					is_master_order_send:'',
					brand_str:[],
					sell_ask:'',
					sell_tolerance:'',
					produce_delivery_time:'',
                    custom_delivery_time:'',
                    has_company:'',
                    has_custom:'',
                    has_component:'',
                    has_orderno:'',
                    has_skc:''
				},
				//验证规则
				rules: {
					apply_name: [
						{required: true, message: '请输入名称'}
					],
					// mobile: [
					// 	{required: true, message: '请输入联系方式'}
					// ],
                    // linkman: [
					// 	{required: true, message: '请输入联系人'}
					// ],
                    referred_as: [
						{required: true, message: '请输入简称'}
					],
				},
				cates: [],  //品牌
                Settlement:[],//结算方式
                muDan:[
                    {id:0,name:'否'},
                    {id:1,name:'是'},
                ],
			}
		},
		mounted() {
            this.getCate()
            this.getSettlement()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						if (this.form.custom_id) {
							res = await this.$API.customer.getCustomerList.edit.post(this.form);
						}else{
							res = await this.$API.customer.getCustomerList.add.post(this.form);
						}
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			setData(data){
                console.log('data :>> ', data);
                this.form.custom_id = data.custom_id
                this.form.apply_name = data.apply_name
                this.form.referred_as = data.referred_as
                this.form.linkman = data.linkman
                this.form.mobile = data.mobile
                this.form.tax_identification_no = data.tax_identification_no
                this.form.custom_delivery_time = data.custom_delivery_time
                this.form.produce_delivery_time = data.produce_delivery_time
                this.form.sell_tolerance = data.sell_tolerance
                this.form.settlement_way = data.settlement_way
                this.form.system_disable = data.system_disable
                this.form.desc = data.desc
                this.form.is_master_order_send = data.is_master_order_send
                this.form.grade = data.grade
                this.form.address = data.address
                this.form.has_company = data.has_company
                this.form.has_custom = data.has_custom
                this.form.has_component = data.has_component
                this.form.has_orderno = data.has_orderno
                this.form.has_skc = data.has_skc
                this.form.brand_str = data.brand_str
                // let a  =[]
                // data.brand_arr.map(item=>{
                //     a.push(Number(item.id))
                // })
                // this.form.brand_str = a
                this.form.remark = data.remark
                this.form.sell_ask = data.sell_ask
			},
            // 获取品牌
            async getCate(){
				var res = await this.$API.customer.getBrand.lists.get();
				this.cates = res.data.rows;
			},
            // 获取结算方式
            async getSettlement(){
				var res = await this.$API.customer.getSettlement.get();
				this.Settlement = res.data;
			},
		}
	}
</script>

<style lang="scss">
.el-form-item--default{
    align-items: center;
}
</style>
